<?php
	$pageTitle = "MyBook+";
	include('./config/config.inc.php');
	function mysqlConnect(){
		include('./config/config.inc.php');
		$db = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
		$db_select = mysql_select_db($mysql_dbname, $db);	
	}
?>
<html>
	<head>
		<title><?php echo $pageTitle; ?></title>
		<link rel="icon" href="./favicon.ico" type="image/x-icon" /> 
		<meta name="description" content="Das beste Social Network der Welt" content="text/html; utf-8"/>
	</head>
	<body>
		<h1>Installation - <?php echo $pageTitle; ?></h1>
		<?php
			if(!isset($_GET['page']) || $_GET['page'] == 1)
			{
				echo '<h2> Config Datei einstellen </h2>';
				echo '1. &Ouml;ffnen Sie die die "config.inc.php" Datei im "Config" Ordner.<br>';
				echo '2. Tragen Sie dort den Host, den Username, das Passwort und den Namen ihrer MySQL Datenbank ein.';
				echo'<br><br> Wenn Sie dies getan haben, k&ouml;nnen Sie auf "Weiter" klicken.';
				echo'<br><br><a href="?page=2"><b>Weiter</b></a>';
			}
			elseif($_GET['page'] == 2)
			{
				echo '<h2> Config Datei einstellen </h2>';
				echo '<h4> Falls Sie hier eine Fehlermeldung bekommen, sollten Sie nochmal ihre Einstellungen &uuml;berpr&uuml;fen.</h4>';
				$db = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
				echo mysql_error($db);
				echo 'Sind die folgenden Daten richtig?';
				echo '<br><br> MySQL Host = '.$mysql_host.'<br>'.'MySQL Username = '.$mysql_user.'<br>'.'MySQL Passwort = '.$mysql_pass.'<br>'.'MySQL Datenbank = '.$mysql_dbname.'<br>';
				echo '<br> Wenn Ja, klicken Sie auf "Weiter".';
				echo'<br><br><a href="?page=1"><b>Zur&uuml;ck</b></a> - <a href="?page=3"><b>Weiter</b></a>';
			}
			elseif($_GET['page'] == 3)
			{
				echo '<h2> Datenbank erstellen </h2>';
				echo 'Klicken Sie auf "Weiter", damit die Tabellen erstellt werden.';
				echo '<br><br><a href="?page=2"><b>Zur&uuml;ck</b></a> - <a href="?page=4"><b>Weiter</b></a>';
				echo '<h3>Code f&uumlr die Datenbank</h3>';
				?>
					<code style="width: 700px; ">
						SET FOREIGN_KEY_CHECKS=0;
						<br>-- ----------------------------
						<br>-- Table structure for accounts
						<br>-- ----------------------------
						<br>CREATE TABLE `accounts` (
						<br>  `id` int(30) NOT NULL auto_increment,
						<br>  `username` varchar(30) NOT NULL,
						<br>  `password` varchar(64) NOT NULL,
						<br>	  `email` varchar(50) NOT NULL,
						<br>	  `admin` tinyint(1) NOT NULL,
						<br>	  PRIMARY KEY  (`id`,`username`),
						<br>	  UNIQUE KEY `a` (`username`)
						<br>	) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for background
						<br>-- ----------------------------
						<br>CREATE TABLE `background` (
						<br>  `userid` varchar(30) NOT NULL,
						<br>	 `background` tinyint(2) NOT NULL,
						<br>  PRIMARY KEY  (`userid`)
						<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for friend_invite
						<br>-- ----------------------------
						<br>CREATE TABLE `friend_invite` (
						<br>  `userid` int(30) NOT NULL default '0',
						<br>  `friendid` int(30) NOT NULL default '0',
						<br>  `blocked` tinyint(1) NOT NULL,
						<br>  PRIMARY KEY  (`userid`,`friendid`)
						<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for friends
						<br>-- ----------------------------
						<br>CREATE TABLE `friends` (
						<br>  `userid` int(30) NOT NULL default '0',
						<br>  `friendid` int(30) NOT NULL default '0',
						<br>  PRIMARY KEY  (`userid`,`friendid`)
						<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for message
						<br>-- ----------------------------
						<br>CREATE TABLE `message` (
						<br>  `id` int(11) NOT NULL auto_increment,
						<br>  `userid` int(11) NOT NULL,
						<br>  `friendid` int(11) NOT NULL,
						<br> `date` varchar(10) NOT NULL,
						<br> `time` varchar(8) NOT NULL,
						<br>  `unread` tinyint(1) NOT NULL,
						<br>  `message` varchar(1000) NOT NULL,
						<br>  PRIMARY KEY  (`id`)
						<br>) ENGINE=MyISAM AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for posts
						<br>-- ----------------------------
						<br>CREATE TABLE `posts` (
						<br>  `id` int(30) NOT NULL auto_increment,
						<br>  `userid` int(30) NOT NULL,
						<br>  `wall` int(30) NOT NULL,
						<br>  `post` varchar(1024) character set latin1 collate latin1_general_ci NOT NULL,
						<br>  `public` tinyint(1) NOT NULL,
						<br>  `date` varchar(10) NOT NULL,
						<br>  `time` time NOT NULL,
						<br>  PRIMARY KEY  (`id`)
						<br>) ENGINE=MyISAM AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for posts_comments
						<br>-- ----------------------------
						<br>CREATE TABLE `posts_comments` (
						<br>  `id` int(30) NOT NULL auto_increment,
						<br>  `postid` int(30) NOT NULL,
						<br>  `userid` int(30) NOT NULL,
						<br>  `comment` varchar(1024) character set latin1 collate latin1_general_ci NOT NULL,
						<br>  `date` varchar(10) NOT NULL,
						<br>  `time` varchar(8) NOT NULL,
						<br>  PRIMARY KEY  (`id`)
						<br>) ENGINE=MyISAM AUTO_INCREMENT=40 DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for profile
						<br>-- ----------------------------
						<br>CREATE TABLE `profile` (
						<br>  `userid` int(20) NOT NULL,
						<br>  `username` varchar(30) NOT NULL,
						<br>  `vorname` varchar(30) NOT NULL,
						<br>  `nachname` varchar(30) NOT NULL,
						<br>  `gender` tinyint(4) NOT NULL,
						<br>  `about` text NOT NULL,
						<br>  `profilepic` varchar(64) NOT NULL,
						<br>  `birthday_day` int(2) NOT NULL,
						<br>  `birthday_month` int(2) NOT NULL,
						<br>  `birthday_year` int(4) NOT NULL,
						<br>  PRIMARY KEY  (`userid`,`username`)
						<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;
						<br>
						<br>-- ----------------------------
						<br>-- Table structure for style
						<br>-- ----------------------------
						<br>CREATE TABLE `style` (
						<br>  `userid` varchar(30) NOT NULL,
						<br>  `style` tinyint(1) NOT NULL,
						<br>  PRIMARY KEY  (`userid`)
						<br>) ENGINE=MyISAM DEFAULT CHARSET=utf8;
					</code>
				<?php
			}
			elseif($_GET['page'] == 4)
			{
				echo '<h2> Datenbank wird erstellt </h2>';
				mysqlConnect();
				$gotError = 0;
				$query = "SET FOREIGN_KEY_CHECKS=0;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				$query = "CREATE TABLE `accounts` (
				  `id` int(30) NOT NULL auto_increment,
				  `username` varchar(30) NOT NULL,
				  `password` varchar(64) NOT NULL,
				  `email` varchar(50) NOT NULL,
				  `admin` tinyint(1) NOT NULL,
				  PRIMARY KEY  (`id`,`username`),
				  UNIQUE KEY `a` (`username`)
				) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `background` (
				  `userid` varchar(30) NOT NULL,
				  `background` tinyint(2) NOT NULL,
				  PRIMARY KEY  (`userid`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `friend_invite` (
				  `userid` int(30) NOT NULL default '0',
				  `friendid` int(30) NOT NULL default '0',
				  `blocked` tinyint(1) NOT NULL,
				  PRIMARY KEY  (`userid`,`friendid`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `friends` (
				  `userid` int(30) NOT NULL default '0',
				  `friendid` int(30) NOT NULL default '0',
				  PRIMARY KEY  (`userid`,`friendid`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `message` (
				  `id` int(11) NOT NULL auto_increment,
				  `userid` int(11) NOT NULL,
				  `friendid` int(11) NOT NULL,
				  `date` varchar(10) NOT NULL,
				  `time` varchar(8) NOT NULL,
				  `unread` tinyint(1) NOT NULL,
				  `message` varchar(1000) NOT NULL,
				  PRIMARY KEY  (`id`)
				) ENGINE=MyISAM AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `posts` (
				  `id` int(30) NOT NULL auto_increment,
				  `userid` int(30) NOT NULL,
				  `wall` int(30) NOT NULL,
				  `post` varchar(1024) character set latin1 collate latin1_general_ci NOT NULL,
				  `public` tinyint(1) NOT NULL,
				  `date` varchar(10) NOT NULL,
				  `time` time NOT NULL,
				  PRIMARY KEY  (`id`)
				) ENGINE=MyISAM AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `posts_comments` (
				  `id` int(30) NOT NULL auto_increment,
				  `postid` int(30) NOT NULL,
				  `userid` int(30) NOT NULL,
				  `comment` varchar(1024) character set latin1 collate latin1_general_ci NOT NULL,
				  `date` varchar(10) NOT NULL,
				  `time` varchar(8) NOT NULL,
				  PRIMARY KEY  (`id`)
				) ENGINE=MyISAM AUTO_INCREMENT=40 DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `profile` (
				  `userid` int(20) NOT NULL,
				  `username` varchar(30) NOT NULL,
				  `vorname` varchar(30) NOT NULL,
				  `nachname` varchar(30) NOT NULL,
				  `gender` tinyint(4) NOT NULL,
				  `about` text NOT NULL,
				  `profilepic` varchar(64) NOT NULL,
				  `birthday_day` int(2) NOT NULL,
				  `birthday_month` int(2) NOT NULL,
				  `birthday_year` int(4) NOT NULL,
				  PRIMARY KEY  (`userid`,`username`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				$query = "CREATE TABLE `style` (
				  `userid` varchar(30) NOT NULL,
				  `style` tinyint(1) NOT NULL,
				  PRIMARY KEY  (`userid`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				mysql_query($query);
				if(mysql_errno())
				{
					echo mysql_errno() . ": " . mysql_error(). "<br>";
					$gotError = 1;
				}
				
				if($gotError == 0)
				{
					echo 'Die Datenbank wurde erfolgreich eingerichtet!';
					echo '<br>Sie k&ouml;nnen nun die Seite aufrufen und verwenden.';
					echo '<br><br><a href="index.php"><b>'.$pageTitle.'</b></a>';
				}
				else
				{
					echo '<br><br>Bitte &uuml;berpr&uuml;fen Sie noch einmal alle Einstellungen!';
				}
			}
		?>
	</body>
</html>